﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace NetworkEasyShare
{
    /// <summary>
    /// Interaction logic for sendFile.xaml
    /// </summary>
    public partial class sendFile : Window
    {
        Transfer file;

        public sendFile()
        {
            InitializeComponent();
            sendButton.IsEnabled = false;
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
                OpenFileDialog openFile = new OpenFileDialog();
                openFile.DefaultExt = "*.*";
                openFile.Filter = "All Files (*.*)|*.*";
                openFile.ShowDialog();
                pathTextBox.Text = null;

                if (openFile.FileNames.Length > 0)
                {
                    foreach (string filename in openFile.FileNames)
                    {
                        pathTextBox.Text = filename;
                    }
                }

                if (pathTextBox.Text != null)
                {
                    FileInfo fileInfo = new FileInfo(pathTextBox.Text);

                    if (fileInfo.Exists)
                    {
                        file = new Transfer(pathTextBox.Text, fileInfo.Name,
                                            fileInfo.Length, fileType(fileInfo.Extension.ToLower()));

                        sendButton.IsEnabled = true;
                        filenameLabelContent.Content = fileInfo.Name;
                        filesizeLabelContent.Content = fileSize(fileInfo.Length, 2);
                        fileTypeLabelContent.Content = fileType(fileInfo.Extension.ToLower());
                    }
                }
        }

        public string fileSize(long size, int decimalPlaces) {

            if (size > 1024 && size < Math.Pow(1024, 2)) {
                return Math.Round(size / Math.Pow(1024, 1), decimalPlaces).ToString() + " KB";
            } else if (size > Math.Pow(1024, 2) && size < Math.Pow(1024, 3)) {
                return Math.Round(size / Math.Pow(1024, 2), decimalPlaces).ToString() + " MB";
            } else if (size > Math.Pow(1024, 3) && size < Math.Pow(1024, 4)) {
                return Math.Round(size / Math.Pow(1024, 3), decimalPlaces).ToString() + " GB";
            } else if (size > Math.Pow(1024, 4) && size < Math.Pow(1024, 5)) {
                return Math.Round(size / Math.Pow(1024, 4), decimalPlaces).ToString() + " MB";
            } else {
                return size.ToString() + " B";
            }
        }

        public string fileType(string extension) {
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension);

            if (key != null && key.GetValue("Content Type") != null)
                return key.GetValue("Content Type").ToString();
            else
                return "application/unknown";
        }

        #region Search Dialog

        private void ShowSearch()
        {
            search searchDialog = new search(file);
            searchDialog.ShowDialog();
            searchDialog = null;
        }

        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            Thread searchThread = new Thread(new ThreadStart(ShowSearch));
            searchThread.SetApartmentState(ApartmentState.STA);
            searchThread.Start();
            this.Hide();
        }
        #endregion
    }
}
